<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    html {
      width: 100%;
      height: 100%;
    }

    body {
      width: 100%;
      height: 2400px;
    }

    div {
      width: 50px;
      height: 50px;
      background-color: blue;
      position: fixed;
      right: 30px;
      bottom: 30px;

      display: none;
    }

    .gotop {
      font-size: 20px;
    }
  </style>
</head>

<body>

  <div class="gotop">返回顶部</div>


  <script src='https://code.jquery.com/jquery-3.6.0.min.js'></script>
  <script>
    $(window).scroll(function () {
      let scrollTop = $(window).scrollTop()
      if (scrollTop > 500) {
        $('.gotop').fadeIn(500)
      } else {
        $('.gotop').fadeOut(500)
      }
    })

    $('.gotop').click(function () {
      $('html,body').animate({
        scrollTop: 0
      }, 1000)
    })
  </script>
</body>

</html>